<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      div {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        top: 30px;
        left: 30px;
      }
    </style>
  </head>
  <body>
    <div></div>

    <script>
      let div = document.querySelector("div");
      div.addEventListener("click", function (e) {
        console.log(e);
        // offsetX, offsetY 事件发生在元素内的位置
        console.log(e.offsetX, e.offsetY, "offset");
        // pageX, pageY 事件发生在客户端document的位置
        console.log(e.pageX, e.pageY, "page");
        // screenX, screenY 事件发生在屏幕相对的位置
        console.log(e.screenX, e.screenY, "screen");
        // clientX, clientY 事件发生在客户端相对的位置
        console.log(e.clientX, e.clientY, "client");
      });
      div.addEventListener("mousedown", function (e) {
        console.log('mousedown  ');
        
        this.onmousemove = function (e) {
          let x = e.pageX;
          let y = e.pageY;
        //   获取元素的宽度/高度
          console.log(this.offsetWidth);
          console.log(this.offsetHeight);
          
          this.style.left = x - this.offsetWidth/2 + "px";
          this.style.top = y - this.offsetHeight/2 + "px";
        };
      });
      div.addEventListener("mouseup", function () {
        this.onmousemove = null
      });
    </script>
  </body>
</html>
